package com.zelin.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zelin.mapper.StudentMapper;
import com.zelin.pojo.Student;
import com.zelin.service.StudentService;
import com.zelin.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;
    @Override
    public List<Student> findAll(){
        return  studentMapper.selectList(null);
    }
   //分页查询
    @Override
    public Page<Student> findByPage(int page, int pageSize) {
        Page<Student> myPage = new Page<>(page,pageSize);
        Wrapper<Student> wrapper = new QueryWrapper<>();
        return studentMapper.selectPage(myPage,wrapper);
    }
   //添加学生
    @Override
    public void insert(Student student) {
         studentMapper.insert(student);
    }
    //修改学生
    @Override
    public void update(Student student) {
          studentMapper.updateById(student);
    }
    //删除学生
    @Override
    public void delete(int sid) {
      studentMapper.deleteById(sid);
    }
    // 条件查询带分页
    @Override
    public Page<Student> search(int page, int pageSize, StudentVo student) {
        //封装查询的分页参数
        Page<Student> myPage = new Page<>(page,pageSize);
        // 封装查询条件
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        // 添加查询条件
        if(student != null){
            if(!StringUtils.isEmpty(student.getSname())){
                wrapper.like("sname",student.getSname());
            }
            if(!StringUtils.isEmpty(student.getAddr())){
                wrapper.like("addr",student.getAddr());
            }
            if( student.getCid() !=0){
                wrapper.eq("cid",student.getCid());
            }
        }
        return studentMapper.selectPage(myPage, wrapper);
    }
}


